package org.jeecg.modules.lims.biz.common.base;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.builder.EqualsBuilder;
import cn.hutool.core.builder.HashCodeBuilder;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

@Getter
@Setter
@ApiModel
public abstract class BaseDomain implements Serializable, Cloneable {

    @ApiModelProperty(value = "创建人id")
    @TableField(fill = FieldFill.INSERT)
    private String createUser;

    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @ApiModelProperty(value = "更新人id")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String updateUser;

    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;


    /**
     *	转成map形式
     */
    public Map<String, Object> toMap() {
        return BeanUtil.beanToMap(this);
    }

    /**
     *	转成map形式 key变成 lower_underscore的下划线形式
     */
    public Map<String, Object> toMapKeyToUnderscore() {
        return BeanUtil.beanToMap(this, true, false);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public String toString() {
        Map<String, Object> map = this.toMap();
        for(Map.Entry<String, Object> entry : map.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof Date) {
                Date dateValue = (Date) value;
                map.replace(entry.getKey(), DateUtil.formatDateTime(dateValue));
            }
        }
        return this.getClass().getSimpleName() + ":" + map.toString();
    }


}
